﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import Display.DialogUI.*;
    import org.aswing.*;
    import Transactions.*;
    import Classes.util.*;
    import flash.filters.*;
    import org.aswing.geom.*;
    import Display.*;
    import Display.MarketUI.*;
    import GameMode.*;
    import Modules.stats.experiments.*;
    import Display.aswingui.*;
    import Engine.*;
    import Modules.stats.types.*;
    import Modules.quest.Helpers.*;

    public class Ship extends HarvestableResource {

        public static const STATE_EMPTY:String = "plowed";
        public static const STATE_AWAY:String = "planted";
        public static const STATE_LOADED:String = "grown";
        public static const IS_SAILING:String = "planting";
        public static const IS_UNLOADING:String = "harvesting";
        public static const IS_CLEANING:String = "plowing";

        private static var shippingLaneX:int = 0;
        private static var shippingLaneYLeft:int = 0;
        private static var shippingLaneYRight:int = 0;
        private static var marketClickedShip:Ship = null;
        private static var intShipNPCCount:int = 0;
        private static var m_catalogWindow:Catalog;

        private const SHIP:String = "ship";
        private const WITHERED_HIGHLIGHT_COLOR:uint = 0xAA0000;

        private var m_itemImageName:String;
        private var m_isNewShip:Boolean = true;
        protected var m_shipNPC:SeaVehicle;
        private var m_PathLength:Number = 0;
        private var m_ReturnTime:Number = 0;
        private var m_ShipReturnTrigger:Boolean = false;

        public function Ship(_arg1:String=null, _arg2:int=0){
            if (shippingLaneX == 0){
                shippingLaneX = Global.gameSettings.getInt("shippingLaneX");
            };
            if (shippingLaneYLeft == 0){
                shippingLaneYLeft = Global.gameSettings.getInt("shippingLaneYLeft");
            };
            if (shippingLaneYRight == 0){
                shippingLaneYRight = Global.gameSettings.getInt("shippingLaneYRight");
            };
            super(_arg1);
            m_objectType = WorldObjectTypes.SHIP;
            m_typeName = this.SHIP;
            this.m_PathLength = 0;
        }
        public static function decrementShipCounter(_arg1:SeaVehicle):void{
            if (intShipNPCCount > 0){
                intShipNPCCount--;
            };
            if (intShipNPCCount == 0){
                Sounds.stop("ship_sailing");
            };
            if (_arg1.isIncoming()){
                Sounds.play("ship_approaches");
            };
        }
        public static function placeFreeShip(_arg1:String, _arg2:Number, _arg3:Number, _arg4:Number):void{
            var _local5:Ship = new Ship(_arg1);
            _local5.setPosition(Math.round(_arg2), Math.round(_arg3), 0);
            _local5.setDirection(Math.round(_arg4));
            _local5.setOuter(Global.world);
            _local5.setVisible(true);
            if (_local5.isValidShipPosition()){
                GameTransactionManager.addTransaction(new TPlaceMapResource(_local5));
                _local5.attach();
            };
        }
        public static function get IsMarketClickedShipSet():Boolean{
            return (!((marketClickedShip == null)));
        }
        private static function SetMarketClickedShip(_arg1:Ship):void{
            marketClickedShip = _arg1;
        }
        public static function ResetMarketClickedShipSet():void{
            marketClickedShip = null;
        }
        public static function SetContactToMarketClickedShip(_arg1:String):void{
            marketClickedShip.plant(_arg1);
            ResetMarketClickedShipSet();
        }

        override public function get harvestingDefinition():Item{
            return (((m_contract)!=null) ? m_contract : m_item);
        }
        override protected function getLayerName():String{
            return (this.getShipLayerBasedOnPierBerth());
        }
        override public function getSaveObject():Object{
            this.m_isNewShip = false;
            var _local1:Object = super.getSaveObject();
            if (m_contract){
                _local1.contractName = m_contract.name;
            };
            return (_local1);
        }
        override public function loadObject(_arg1:Object):void{
            this.m_isNewShip = false;
            m_contract = Global.gameSettings.getItemByName(_arg1.contractName);
            m_contractGrowTime = (m_contract) ? m_contract.growTime : 0;
            super.loadObject(_arg1);
        }
        public function isUnloadable():Boolean{
            return (super.isHarvestable());
        }
        public function isEmpty():Boolean{
            return (super.isPlantable(null));
        }
        public function isAway():Boolean{
            return (super.isPlanted());
        }
        public function isCleanable():Boolean{
            return (super.isPlowable());
        }
        public function isLoaded():Boolean{
            return (super.isGrown());
        }
        public function goodsRunInProgress():Boolean{
            return (!((this.m_shipNPC == null)));
        }
        override protected function getCurrentImage():ItemImageInstance{
            return (super.getCurrentImage());
        }
        private function resetNPCPathData():void{
            this.m_PathLength = 0;
            this.m_ReturnTime = 0;
            this.m_ShipReturnTrigger = false;
        }
        override public function setFullGrown():void{
            super.setFullGrown();
            this.cancelShipNPC();
        }
        override protected function getPlowedCursor():Class{
            return (EmbeddedArt.hud_biz_supply);
        }
        override protected function getGrownCursor():Class{
            if (((!(m_isHarvesting)) && (Global.player.checkEnergy(-(m_contract.harvestEnergyCost), false)))){
                return (EmbeddedArt.hud_biz_supply);
            };
            return (null);
        }
        override public function get isHighlightable():Boolean{
            return (((super.isHighlightable) && (((!((m_state == STATE_PLANTED))) || (Global.world.isEditMode)))));
        }
        override protected function getWitheredCursor():Class{
            return (EmbeddedArt.hud_act_clean);
        }
        override protected function isWrongPlantContract():Boolean{
            var _local1:GMPlant = GMPlant(Global.world.getTopGameMode());
            if (_local1.m_contractClass == "ship_contract"){
                return (false);
            };
            return (true);
        }
        override public function getHighlightColor():uint{
            if (m_state == STATE_WITHERED){
                return (this.WITHERED_HIGHLIGHT_COLOR);
            };
            return (super.getHighlightColor());
        }
        protected function onContractImageLoaded(_arg1:LoaderEvent):void{
            _arg1.target.removeEventListener(LoaderEvent.LOADED, this.onContractImageLoaded);
            reloadImage();
        }
        override public function getToolTipHeader():String{
            var _local1:String = this.getContractFriendlyName();
            if (!_local1){
                if ((Global.world.getTopGameMode() is GMPlant)){
                    if (this.isWrongPlantContract()){
                        return (null);
                    };
                };
                return (super.getToolTipHeader());
            };
            return (_local1);
        }
        override public function getCustomCursor():Object{
            var _local1:DisplayObject;
            var _local2:MarginBackground;
            var _local3:ASFont;
            var _local4:GlowFilter;
            if ((((m_state == STATE_AWAY)) && (!((Global.world.getTopGameMode() is GMPlant))))){
                if (!m_customCursor){
                    m_customCursor = ASwingHelper.makeSoftBoxJPanel(SoftBoxLayout.CENTER);
                    m_customCursorLabel = new JLabel("");
                    m_customCursorHolder = new Sprite();
                    Global.ui.addChild(m_customCursorHolder);
                    m_customCursorWindow = new JWindow(m_customCursorHolder);
                    _local3 = ASwingHelper.getBoldFont(16);
                    _local4 = new GlowFilter(0, 1, 1.2, 1.2, 20, BitmapFilterQuality.HIGH);
                    m_customCursorLabel.setForeground(new ASColor(0xFFFFFF));
                    m_customCursorLabel.setFont(_local3);
                    m_customCursorLabel.setTextFilters([_local4]);
                };
                _local1 = null;
                _local1 = (new EmbeddedArt.hud_act_zap() as DisplayObject);
                m_customCursorLabel.setText(m_contract.instantFinishEnergyCost.toString());
                m_customCursorLabel.visible = true;
                _local2 = new MarginBackground(_local1, new Insets(0, 0, 0, 0));
                m_customCursor.setBackgroundDecorator(_local2);
                m_customCursor.setPreferredSize(new IntDimension(_local1.width, _local1.height));
                m_customCursor.setMinimumSize(new IntDimension(_local1.width, _local1.height));
                m_customCursor.setMaximumSize(new IntDimension(_local1.width, _local1.height));
                m_customCursor.append(m_customCursorLabel);
                m_customCursorWindow.setContentPane(m_customCursor);
                ASwingHelper.prepare(m_customCursorWindow);
                m_customCursorWindow.show();
                return (m_customCursorWindow);
            };
            return (null);
        }
        override public function getToolTipAction():String{
            var _local1:String = super.getToolTipAction();
            if (Global.isVisiting()){
                if (Global.player.checkVisitorEnergy(1)){
                    switch (m_state){
                        case STATE_LOADED:
                            _local1 = ZLoc.t("Main", "LoadedAction");
                            break;
                        case STATE_AWAY:
                            _local1 = ZLoc.t("Main", "AwayShipVisitor");
                            break;
                        case STATE_WITHERED:
                            _local1 = ZLoc.t("Main", "WitheredGoodsActionVisit");
                            break;
                    };
                } else {
                    _local1 = ZLoc.t("Main", "NeedEnergy", {amount:1});
                };
            } else {
                if (m_state == STATE_WITHERED){
                    _local1 = ZLoc.t("Main", "WitheredGoodsAction");
                };
                if (m_state == STATE_LOADED){
                    if (!Global.player.checkEnergy(-(m_item.harvestEnergyCost), false)){
                        _local1 = ZLoc.t("Main", "NeedEnergy", {amount:m_item.harvestEnergyCost});
                    } else {
                        _local1 = ZLoc.t("Main", "LoadedAction");
                    };
                };
                if (m_state == STATE_EMPTY){
                    if ((Global.world.getTopGameMode() is GMPlant)){
                        if (this.isWrongPlantContract()){
                            return (null);
                        };
                    };
                    _local1 = ZLoc.t("Main", "EmptyShipAction");
                };
            };
            return (_local1);
        }
        override public function onPlayAction():void{
            var _local1:Number;
            if (m_visitReplayLock > 0){
                return;
            };
            super.onPlayAction();
            if (Global.isVisiting()){
                return;
            };
            if (!hasValidId()){
                showObjectBusy();
                return;
            };
            m_actionMode = PLAY_ACTION;
            switch (m_state){
                case STATE_EMPTY:
                    if (Global.guide.isActive()){
                        break;
                    };
                    if (UI.resolveIfAssetsHaveNotLoaded(DelayedAssetLoader.MARKET_ASSETS)){
                        break;
                    };
                    SetMarketClickedShip(this);
                    m_catalogWindow = UI.displayCatalog("ship_contract", null, true, "ship_contract", true, false, true);
                    if (!m_catalogWindow.hasEventListener(Event.CLOSE)){
                        m_catalogWindow.addEventListener(Event.CLOSE, this.onMarketCloseClick);
                    };
                    break;
                case STATE_AWAY:
                    this.processAwayState();
                    break;
                case STATE_LOADED:
                    _local1 = m_contract.harvestEnergyCost;
                    if (!Global.player.checkEnergy(-(_local1))){
                        displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
                    } else {
                        if (!m_isHarvesting){
                            if (!isHarvestable()){
                                break;
                            };
                            if (((m_firstHarvestAttempt) && (Global.player.commodities.isAtCommodityCapacity(m_contract)))){
                                m_firstHarvestAttempt = false;
                                break;
                            };
                            m_isHarvesting = true;
                            setHighlighted(false);
                            Global.world.citySim.pickupManager.enqueue("NPC_farmer", this);
                        };
                    };
                    break;
                case STATE_FALLOW:
                case STATE_WITHERED:
                    this.plow();
                    break;
            };
        }
        override public function onVisitPlayAction():void{
            m_actionMode = VISIT_PLAY_ACTION;
            switch (m_state){
                case STATE_AWAY:
                    Global.world.citySim.pickupManager.enqueue("NPC_farmer", this);
                    break;
                case STATE_LOADED:
                    if (isHarvestable()){
                        m_isHarvesting = true;
                        setHighlighted(false);
                        Global.world.citySim.pickupManager.enqueue("NPC_farmer", this);
                    };
                    break;
                case STATE_WITHERED:
                    Global.world.citySim.pickupManager.enqueue("NPC_farmer", this);
                    break;
            };
        }
        override public function onVisitReplayAction(_arg1:TRedeemVisitorHelpAction):Number{
            m_actionMode = VISIT_REPLAY_ACTION;
            switch (m_state){
                case STATE_AWAY:
                    break;
                case STATE_LOADED:
                    if (isHarvestable()){
                        m_isHarvesting = true;
                        setHighlighted(false);
                    };
                    break;
                case STATE_WITHERED:
                    break;
                default:
                    return (0);
            };
            GameTransactionManager.addTransaction(_arg1);
            getProgressBarEndFunction()();
            return (super.onVisitReplayAction(null));
        }
        override public function isVisitorInteractable():Boolean{
            switch (m_state){
                case STATE_AWAY:
                    return (true);
                case STATE_LOADED:
                    if (((isHarvestable()) && (!(Global.player.commodities.isAtCommodityCapacity(m_contract))))){
                        return (true);
                    };
                    return (false);
                case STATE_WITHERED:
                    return (true);
            };
            return (false);
        }
        protected function cancelContractHandler(_arg1:GenericPopupEvent):void{
            if (_arg1.button != GenericPopup.YES){
                return;
            };
            GameTransactionManager.addTransaction(new TFactoryContractCancel(this));
            this.cancelShipNPC();
            if ((m_contract is Item)){
                displayStatus(ZLoc.t("Main", "ShipCancelContractSuccess", {crop:m_contract.localizedName}));
                m_contract = null;
            };
            setState(STATE_EMPTY);
        }
        protected function onMarketCloseClick(_arg1:Event):void{
            if (m_catalogWindow){
                m_catalogWindow.removeEventListener(Event.CLOSE, this.onMarketCloseClick);
            };
            m_catalogWindow = null;
        }
        override public function plow():Boolean{
            var _local2:int;
            var _local3:Number;
            var _local4:Number;
            var _local5:int;
            var _local6:int;
            var _local1:Boolean;
            if (isWithered()){
                if (m_contract){
                    _local2 = Global.gameSettings.getInt("plowXp");
                    _local3 = Global.gameSettings.getNumber("witherRefundMultiplier", 0);
                    _local4 = Math.floor((m_contract.cost * _local3));
                    doResourceChanges(0, _local4, _local2);
                };
                Sounds.play("ships_unloaded");
                setState(STATE_EMPTY);
                GameTransactionManager.addTransaction(new TClearWithered(this));
                setHighlighted(true);
                trackAction(TrackedActionType.CLEAR);
                return (true);
            };
            if (this.isCleanable()){
                _local5 = Global.gameSettings.getInt("plowCost");
                _local6 = Global.gameSettings.getInt("plowXp");
                if (Global.player.canBuy(_local5)){
                    doResourceChanges(0, -(_local5), _local6);
                    setState(STATE_EMPTY);
                    GameTransactionManager.addTransaction(new TPlow(this), true);
                    _local1 = true;
                } else {
                    if (((this.m_isNewShip) && ((m_state == STATE_FALLOW)))){
                        detach();
                    };
                };
            };
            return (_local1);
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
            if (this.isAway()){
                this.calculateShipReturnTime();
                this.checkAndDoShipReturns();
            };
        }
        override public function harvest():Boolean{
            m_isHarvesting = false;
            if (!this.isUnloadable()){
                return (false);
            };
            if (((!(Global.isVisiting())) && (m_firstHarvestAttempt))){
                if (Global.player.commodities.isAtCommodityCapacity(m_contract)){
                    m_firstHarvestAttempt = false;
                    return (false);
                };
            };
            m_firstHarvestAttempt = true;
            Sounds.play("ships_unloaded");
            if (!Global.isVisiting()){
                m_doobersArray = Global.player.processRandomModifiers(this.harvestingDefinition, this, true, m_secureRands);
                Global.world.dooberManager.createBatchDoobers(m_doobersArray, this.harvestingDefinition, m_position.x, m_position.y);
                if (m_actionMode != VISIT_REPLAY_ACTION){
                    GameTransactionManager.addTransaction(new THarvest(this));
                    trackAction(TrackedActionType.HARVEST);
                    Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
                };
            } else {
                onVisitorHelp(STATE_GROWN);
            };
            setState(STATE_EMPTY);
            plantTime = 0;
            return (true);
        }
        override public function revive():Boolean{
            NeighborVisitManager.setVisitFlag(NeighborVisitManager.VISIT_CLEANEDSHIPS);
            return (super.revive());
        }
        override public function getCoinYield():int{
            var _local1:Number = ((m_contract)!=null) ? m_contract.coinYield : m_item.coinYield;
            return (Math.floor(_local1));
        }
        override public function getXpYield():int{
            var _local1:Number = ((m_contract)!=null) ? m_contract.xpYield : m_item.xpYield;
            return (Math.floor(_local1));
        }
        override protected function friendVisitRepGain():int{
            return (Global.gameSettings.getInt("friendVisitShipRepGain", 1));
        }
        override public function doHarvestDropOff(_arg1:Boolean=true):Object{
            if (((_arg1) && (m_contract))){
                displayDelayedResourceChanges();
            };
            m_contract = null;
            return (super.doHarvestDropOff(_arg1));
        }
        override public function plant(_arg1:String):Boolean{
            var _local3:Item;
            var _local4:int;
            var _local5:int;
            var _local2:Boolean;
            if (this.isEmpty()){
                _local3 = Global.gameSettings.getItemByName(_arg1);
                if (((_local3) && ((_local3.type == "ship_contract")))){
                    _local4 = _local3.cost;
                    _local5 = _local3.plantXp;
                    if (Global.player.canBuy(_local4)){
                        Global.player.gold = (Global.player.gold - _local4);
                        plantTime = GlobalEngine.getTimer();
                        Sounds.play("ship_leaves");
                        m_contract = _local3;
                        m_contractGrowTime = _local3.growTime;
                        m_witherOn = _local3.allowWither;
                        setState(STATE_AWAY);
                        GameTransactionManager.addTransaction(new TFactoryContractStart(this), true);
                        trackAction(TrackedActionType.PLANT);
                        this.startEmptyShipNPC();
                        _local2 = true;
                    };
                };
            } else {
                if (!hasValidId()){
                    showObjectBusy();
                };
            };
            if (_local2){
                this.adjustModesAfterPlanting();
            };
            return (_local2);
        }
        protected function adjustModesAfterPlanting():void{
        }
        override protected function getContractFriendlyName():String{
            if (!m_contract){
                return ("");
            };
            return (ZLoc.t("Items", (m_contract.name + "_friendlyName")));
        }
        override public function getToolTipStatus():String{
            var _local2:String;
            var _local3:int;
            var _local1:String;
            if (((!(Global.isVisiting())) && (!(Global.world.isEditMode)))){
                switch (m_state){
                    case STATE_EMPTY:
                        _local1 = ZLoc.t("Main", "EmptyShip");
                        break;
                    case STATE_AWAY:
                        _local2 = GameUtil.formatMinutesSeconds(getGrowTimeLeft());
                        _local1 = ZLoc.t("Main", "AwayShip", {time:_local2});
                        break;
                    case STATE_LOADED:
                        _local1 = ZLoc.t("Main", "LoadedShip");
                        break;
                    case STATE_WITHERED:
                        _local3 = Global.gameSettings.getInt("plowCost");
                        if (Global.player.canBuy(_local3, false)){
                            _local1 = ZLoc.t("Main", "WitheredGoods");
                        } else {
                            _local1 = ZLoc.t("Main", "WitheredGoods_NoMoney");
                        };
                        break;
                };
            };
            return (_local1);
        }
        public function connectedToPier(_arg1:Pier):Boolean{
            var _local3:Pier;
            if (!_arg1.isValidShipBerth(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y))){
                return (false);
            };
            var _local2:Array = Global.world.getObjectsByClass(Pier);
            for each (_local3 in _local2) {
                if (_local3 == _arg1){
                } else {
                    if (_local3.isValidShipBerth(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y))){
                        return (false);
                    };
                };
            };
            return (true);
        }
        private function checkCollisionWithShipSquare(_arg1:Number, _arg2:Number):Boolean{
            var _local4:int;
            var _local3:int = m_position.x;
            while (_local3 < (m_position.x + m_size.x)) {
                _local4 = m_position.y;
                while (_local4 < (m_position.y + m_size.y)) {
                    if ((((_arg1 == _local3)) && ((_arg2 == _local4)))){
                        return (true);
                    };
                    _local4++;
                };
                _local3++;
            };
            return (false);
        }
        public function collidedWithShip(_arg1:Rectangle):Boolean{
            var _local3:int;
            var _local2:int = _arg1.left;
            while (_local2 < (_arg1.left + _arg1.width)) {
                _local3 = _arg1.top;
                while (_local3 < (_arg1.top + _arg1.height)) {
                    if (this.checkCollisionWithShipSquare(_local2, _local3)){
                        return (true);
                    };
                    _local3++;
                };
                _local2++;
            };
            return (false);
        }
        public function isValidShipPosition():Boolean{
            var _local3:Pier;
            var _local4:Ship;
            var _local5:Pier;
            var _local1:Array = Global.world.getObjectsByClass(Pier);
            var _local2:Array = Global.world.getObjectsByClass(Ship);
            for each (_local3 in _local1) {
                if (_local3.shipCollidedWithDock(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y))){
                    return (false);
                };
            };
            for each (_local4 in _local2) {
                if (_local4 == this){
                } else {
                    if (_local4.collidedWithShip(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y))){
                        return (false);
                    };
                };
            };
            for each (_local5 in _local1) {
                if (_local5.isValidShipBerth(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y))){
                    return (true);
                };
            };
            return (false);
        }
        public function forceSell():void{
            super.sellNow();
            this.cancelShipNPC();
        }
        public function forceMove(_arg1:Vector3):void{
            var _local2:MapResource = (this as MapResource);
            var _local3:Object = new Object();
            _local3.x = Math.round((this.m_position.x - _arg1.x));
            _local3.y = Math.round((this.m_position.y - _arg1.y));
            _local3.state = _local2.getState();
            _local3.direction = _local2.getDirection();
            _local3 = _local2.addTMoveParams(_local3);
            GameTransactionManager.addTransaction(new TMove(_local2, _local3));
            this.setPosition(_local3.x, _local3.y);
            this.conditionallyReattach();
            if (((this.m_shipNPC) && (this.m_ShipReturnTrigger))){
                this.cancelShipNPC();
            };
        }
        override public function onObjectDrop(_arg1:Vector3):void{
            super.onObjectDrop(_arg1);
            this.resetShipGameLayer();
            var _local2:GameMode = Global.world.getTopGameMode();
        }
        private function getShippingLaneExitY(_arg1:Number):Number{
            if (_arg1 < 0){
                return (shippingLaneYRight);
            };
            return (shippingLaneYLeft);
        }
        private function calculateShipReturnTime():void{
            var _local1:Array;
            var _local2:Vector3;
            var _local3:Vector3;
            var _local4:NPC;
            if (this.m_PathLength == 0){
                _local1 = new Array();
                _local1.push(new Vector3(shippingLaneX, this.getShippingLaneExitY(this.m_position.y), 0));
                _local1.push(new Vector3(shippingLaneX, this.m_position.y, 0));
                _local1.push(new Vector3(this.m_position.x, this.m_position.y, 0));
                _local2 = new Vector3(shippingLaneX, this.getShippingLaneExitY(this.m_position.y), 0);
                for each (_local3 in _local1) {
                    this.m_PathLength = (this.m_PathLength + MathUtil.distance(new Point(_local3.x, _local3.y), new Point(_local2.x, _local2.y)));
                    _local2 = _local3;
                };
                _local4 = new NPC(this.m_item.loadedNPCType, false);
                this.m_ReturnTime = (this.m_PathLength / _local4.velocityWalk);
            };
        }
        private function checkAndDoShipReturns():void{
            var _local1:Number;
            var _local2:Number;
            var _local3:Number;
            if (plantTime > 0){
                _local1 = (GlobalEngine.getTimer() - plantTime);
                _local2 = getGrowTimeDelta();
                if (_local1 < _local2){
                    _local3 = ((_local2 - _local1) / 1000);
                    if ((((_local3 < this.m_ReturnTime)) && (!(this.m_ShipReturnTrigger)))){
                        this.startLoadedShipNPC();
                    };
                };
            };
        }
        private function startEmptyShipNPC():void{
            var _local1:Array = new Array();
            _local1.push(new Vector3(shippingLaneX, this.m_position.y, 0));
            _local1.push(new Vector3(shippingLaneX, this.getShippingLaneExitY(this.m_position.y), 0));
            this.m_shipNPC = Global.world.citySim.npcManager.createShipByNameAtPosition(this.m_item.emptyNPCType, this.m_position, _local1, false);
            intShipNPCCount++;
        }
        public function playShipLoopSound():Boolean{
            return ((intShipNPCCount > 0));
        }
        private function startLoadedShipNPC():void{
            this.cancelShipNPC();
            this.m_ShipReturnTrigger = true;
            var _local1:Vector3 = new Vector3(shippingLaneX, this.getShippingLaneExitY(this.m_position.y), 0);
            var _local2:Array = new Array();
            _local2.push(new Vector3(shippingLaneX, this.m_position.y, 0));
            _local2.push(new Vector3(this.m_position.x, this.m_position.y, 0));
            this.m_shipNPC = Global.world.citySim.npcManager.createShipByNameAtPosition(this.m_item.loadedNPCType, _local1, _local2, false);
            this.m_shipNPC.setIsIncoming();
            intShipNPCCount++;
        }
        private function cancelShipNPC():void{
            if (this.goodsRunInProgress()){
                Global.world.citySim.npcManager.removeShip(this.m_shipNPC);
                this.resetNPCPathData();
                this.m_shipNPC = null;
            };
        }
        override public function getActionText():String{
            if ((((m_state == STATE_PLANTED)) && (!((((m_actionMode == VISIT_PLAY_ACTION)) || ((m_actionMode == VISIT_REPLAY_ACTION))))))){
                return (getFinishingText());
            };
            return (super.getActionText());
        }
        override public function getWateringText():String{
            return (ZLoc.t("Main", "Guiding"));
        }
        override public function getRevivingText():String{
            return (ZLoc.t("Main", "Salvaging"));
        }
        override public function getHarvestingText():String{
            return (ZLoc.t("Main", "Unloading"));
        }
        override public function getHarvestTime():Number{
            return (Global.gameSettings.getNumber("actionBarPlotSpeedup"));
        }
        override public function getProgressBarStartFunction():Function{
            var harvestResource:* = null;
            harvestResource = this;
            return (function ():Boolean{
                var _local1:*;
                if (!Global.isVisiting()){
                    if (m_actionMode != VISIT_REPLAY_ACTION){
                        switch (m_state){
                            case STATE_AWAY:
                                break;
                            case STATE_LOADED:
                                _local1 = m_contract.harvestEnergyCost;
                                if (m_actionMode == VISIT_REPLAY_ACTION){
                                    _local1 = 0;
                                };
                                if (!Global.player.checkEnergy(-(_local1))){
                                    return (false);
                                };
                                if (Global.player.commodities.isAtCommodityCapacity(m_contract)){
                                    return (false);
                                };
                                Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                                doEnergyChanges(-(_local1), new Array("energy", "expenditures", "harvest_ship", harvestingDefinition.name));
                                Global.player.heldEnergy = (Global.player.heldEnergy + _local1);
                                m_heldEnergy = _local1;
                                break;
                        };
                    } else {
                        Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                    };
                } else {
                    switch (m_state){
                        case STATE_AWAY:
                            m_lastVisitorHelpType = VisitorHelpType.SHIP_WATER;
                            m_numVisitorInteractions++;
                            Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                            break;
                        case STATE_LOADED:
                            if (Global.player.commodities.isAtCommodityCapacity(m_contract)){
                                return (false);
                            };
                            m_lastVisitorHelpType = VisitorHelpType.SHIP_HARVEST;
                            m_numVisitorInteractions++;
                            Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                            break;
                        case STATE_WITHERED:
                            m_lastVisitorHelpType = VisitorHelpType.SHIP_REVIVE;
                            m_numVisitorInteractions++;
                            Sounds.playFromSet(Sounds.SET_HARVEST, harvestResource);
                            break;
                    };
                };
                return (true);
            });
        }
        override public function getProgressBarCancelFunction():Function{
            return (function ():void{
                m_isHarvesting = false;
                Global.player.heldEnergy = (Global.player.heldEnergy - m_heldEnergy);
            });
        }
        private function getShipLayerBasedOnPierBerth():String{
            var _local2:Pier;
            var _local1:Array = Global.world.getObjectsByClass(Pier);
            for each (_local2 in _local1) {
                if (_local2.getShipLayer(new Rectangle(m_position.x, m_position.y, m_size.x, m_size.y)) == 0){
                    return ("backship");
                };
            };
            return ("frontship");
        }
        public function resetShipGameLayer():void{
            this.detatch();
            this.attach();
        }
        override public function getVisitReplayEquivalentActionString():String{
            var _local1 = "";
            switch (this.getState()){
                case STATE_LOADED:
                    _local1 = "harvest";
                    break;
                case STATE_AWAY:
                case STATE_WITHERED:
                    break;
            };
            return (_local1);
        }
        private function processAwayState():void{
            var _local2:DisplayObject;
            var _local1:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_INSTANT_READY_CROPS_SHIPS);
            if (((((!(Global.isVisiting())) && ((Global.player.level >= 6)))) && ((_local1 > 1)))){
                _local2 = new InstantReadyDialog(this);
                UI.displayPopup(_local2, true, "instantReadyShips", false);
            };
        }

    }
}//package Classes 
